//package controllers.admin;
//
//import java.io.File;
//import java.util.Arrays;
//import java.util.Iterator;
//import java.util.List;
//
//import models.admin.FiscalCycle;
//import models.admin.Indicator;
//import models.admin.Project;
//import models.admin.Role;
//import play.i18n.Messages;
//import play.mvc.Util;
//import play.mvc.With;
//import controllers.BaseController;
//import controllers.Check;
//import controllers.Secure;
//
///**
// * @author rjanixz
// *
// */
//@With(Secure.class)
//public class Projects extends BaseController {
//
//	
//	@Check(value="VIEW")
//	public static void add() {
//		List<FiscalCycle> fiscalCycles = Arrays.asList(FiscalCycle.values());
//		render(fiscalCycles);
//	}
//	
//	@Check(value="EDIT")
//	public static void edit(Long id) {
//		final List<FiscalCycle> fiscalCycles = Arrays.asList(FiscalCycle.values());
//		final List<Role> roles = Role.findAll();
//		final Project project = Project.findById(id);
//		render(fiscalCycles,project,roles);
//	}
//	
//	@Check(value="ADD")
//	public static void save(Project project) {
//		project.save();
//		redirect("/admin/projects/all");
//	}
//
//	@Check(value="EDIT")
//	public static void update(Project project) {
//		project.save();
//		redirect("/admin/projects/all");
//	}
//	
//	@Check(value="EDIT")
//	public static void addDetail(Long projectId, Long roleId) {
//		Project project = Project.findById(projectId);
//		Role role = Role.findById(roleId);
//		project.getRoles().add(role);
//		project.save();
//		redirect("/admin/projects/edit/"+projectId);
//	}
//	
//	@Check(value="EDIT")
//	public static void removeDetail(Long projectId, Long roleId) {
//		Project project = Project.findById(projectId);
//		Role role = Role.findById(roleId);
//		project.getRoles().remove(role);
//		project.save();
//		redirect("/admin/projects/edit/"+projectId);
//	}
//	
//	@Check(value="EDIT")
//	public static void indicators() {
//		render();
//	}
//	
//	public static void indicatorsList() {
//		List<Indicator> indicators = Indicator.find("parent is null").fetch();
//		
//		StringBuilder builder = new StringBuilder();
//		int level = 0;
//		builder.append("[");
//		
//		int currentId=1;
//		
//		for (Iterator<Indicator> iterator = indicators.iterator(); iterator.hasNext();) {
//			Indicator indicator = iterator.next();
//			
//			builder.append(indicator.toJSON(level,currentId,null));
//			Long parentId = new Long (currentId);
//			if (indicator.getChildren() != null) {
//				currentId = getChildren(builder,level, currentId,parentId, indicator);
//			}
//			
//			if (iterator.hasNext()) {
//				builder.append(",");
//			}
//		}
//
//		builder.append("]");
//		renderJSON(builder.toString());
//	}
//	
//	public static void indicatorsTreeList(Long indicatorId) {
//		
//		List<Indicator> indicators = Indicator.find("parent is null").fetch();
//		StringBuilder builder = new StringBuilder();
//		builder.append("[");
//		
//		for (Iterator<Indicator> iterator = indicators.iterator(); iterator.hasNext();) {
//			Indicator indicator = iterator.next();
//			
//			if (indicator.getChildren().isEmpty()) {
//				builder.append(indicator.toTreeJSON(null));
//			} else {
//				String branch = getTreeChildren(indicator);
//				builder.append(indicator.toTreeJSON(branch));
//			}
//			
//			if (iterator.hasNext()) {
//				builder.append(",");
//			}
//		}
//		builder.append("]");
//		renderJSON(builder.toString());
//	}
//	
//	@Util
//	private static String getTreeChildren(Indicator parent) {
//		StringBuilder builder = new StringBuilder();
//		
//		for (Iterator<Indicator> iterator = parent.getChildren().iterator(); iterator.hasNext();) {
//			Indicator indicator = iterator.next();
//			
//			if (indicator.getChildren().isEmpty()) {
//				builder.append(indicator.toTreeJSON(null));
//			} else {
//				String branch = getTreeChildren(indicator);
//				builder.append(indicator.toTreeJSON(branch));
//			}
//			
//			if (iterator.hasNext()) {
//				builder.append(",");
//			}
//		}
//		
//		return builder.toString();
//	}
//	
//	@Util
//	private static int getChildren(StringBuilder builder, int level, int currentId, Long parentId, Indicator parent) {
//		int nextId = currentId + 1;
//		for (Iterator<Indicator> iterator = parent.getChildren().iterator(); iterator.hasNext();) {
//			Indicator indicator = iterator.next();
//			builder.append(",");
//			builder.append(indicator.toJSON(level+1, nextId,parentId));
//			Long childParentId = new Long (nextId);
//			if (indicator.getChildren() != null) {
//				nextId = getChildren(builder,level+1,nextId,childParentId,indicator);
//			}
//		}
//		return nextId;
//	}
//	
//	public static void loadIndicators(File file) {
//		
//		redirect("/admin/projects/indicators");
//	}
//	
//	public static void editIndicator(String oper, Indicator indicator, Long newParentId, Long id) {
//		boolean success = true;
//		switch (oper) {
//			case "add": case "edit":
//				try {
//					Indicator indicatorQuery = Indicator.find("name = ? or numeral = ?", indicator.getName(), indicator.getNumeral()).first();
//					indicator.save();
//					if(indicatorQuery == null){
//						flash.success(Messages.get("indicator.create.success"));
//					}else{
//						flash.success(Messages.get("indicator.update.success"));
//					}
//				} catch (Exception e) {
//					flash.error(Messages.get("indicator.update.fail"));
//				}
//				break;
//			case "del":
//				Indicator indicatorToDelete = Indicator.findById(id);
//				indicatorToDelete.delete();
//				break;
//			case "move":
//				if(id != newParentId){
//					if(newParentId == 0){
//						Indicator childIndicator = Indicator.findById(id);
//						childIndicator.setParent(null);
//						childIndicator.save();
//					}else{
//						Indicator childIndicator = Indicator.findById(id);
//						Indicator parentIndicator = Indicator.findById(newParentId);
//						childIndicator.setParent(parentIndicator);
//						childIndicator.save();
//					}
//				}else{
//					flash.error(Messages.get("indicator.changeParent.fail"));
//				}
//				break;
//			default:
//				break;
//		}
//		
////		System.out.println("New Numeral: "+numeral);
////		System.out.println("Old Numeral: "+indicator.getNumeralLabel());
////		
////		if (!indicator.getNumeralLabel().equals(numeral)) {
////			System.out.println("***** Numeral has changed");
////			if (numeral.matches("\\d(\\.\\d)+")) {
////				System.out.println("****** label has the valid pattern");
////				String[] levels = numeral.split("\\.");
////				for (String level : levels) {
////					System.out.println("lvl: " + level);
////				}
////			} else {
////				System.out.println("****** label has not the valid patthern");
////				success = false;
////			}
////			
////		}
////		
//		renderJSON(success);
//	}
//	 
//}
